import Vue from 'vue';
import Vuex from 'vuex';

import { INCREMENT } from '@/store/mutationTypes';

import number from '@/store/number';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 100,
  },

  // 改变状态
  mutations: {
    [INCREMENT](state, payload) {
      state.count += payload.num;
    },
    decrement(state) {
      setTimeout(() => {
        state.count -= 2;
      }, 2000);
    },
  },
  actions: {
    increment(context) {
      // 执行多次
      context.commit(INCREMENT, { num: 10 });

      setTimeout(() => {
        context.commit('decrement');
      }, 2000);
    },
    decrement({ commit, dispatch, rootState }) {
      console.log(rootState);
      commit(INCREMENT, { num: 3 });
      dispatch('increment');
    },
  },
  modules: {
    number,
  },
});
